/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.basicdata.standard.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.basicdata.standard.entity.BasicdataCheckStandardMain;
import com.jeeplus.modules.basicdata.standard.mapper.BasicdataCheckStandardMainMapper;
import com.jeeplus.modules.basicdata.standard.entity.BasicdataCheckForm;
import com.jeeplus.modules.basicdata.standard.mapper.BasicdataCheckFormMapper;

/**
 * 设备点检标准Service
 * @author 王志鹏
 * @version 2020-09-25
 */
@Service
@Transactional(readOnly = true)
public class BasicdataCheckStandardMainService extends CrudService<BasicdataCheckStandardMainMapper, BasicdataCheckStandardMain> {

	@Autowired
	private BasicdataCheckFormMapper basicdataCheckFormMapper;

	public BasicdataCheckStandardMain get(String id) {
		BasicdataCheckStandardMain basicdataCheckStandardMain = super.get(id);
		basicdataCheckStandardMain.setBasicdataCheckFormList(basicdataCheckFormMapper.findList(new BasicdataCheckForm(basicdataCheckStandardMain)));
		return basicdataCheckStandardMain;
	}

	public List<BasicdataCheckStandardMain> findList(BasicdataCheckStandardMain basicdataCheckStandardMain) {
		return super.findList(basicdataCheckStandardMain);
	}

	public Page<BasicdataCheckStandardMain> findPage(Page<BasicdataCheckStandardMain> page, BasicdataCheckStandardMain basicdataCheckStandardMain) {
		return super.findPage(page, basicdataCheckStandardMain);
	}

	@Transactional(readOnly = false)
	public void save(BasicdataCheckStandardMain basicdataCheckStandardMain) {
		super.save(basicdataCheckStandardMain);
		for (BasicdataCheckForm basicdataCheckForm : basicdataCheckStandardMain.getBasicdataCheckFormList()){
			if (basicdataCheckForm.getId() == null){
				continue;
			}
			if (BasicdataCheckForm.DEL_FLAG_NORMAL.equals(basicdataCheckForm.getDelFlag())){
				if (StringUtils.isBlank(basicdataCheckForm.getId())){
					basicdataCheckForm.setCheckName(basicdataCheckStandardMain);
					basicdataCheckForm.preInsert();
					basicdataCheckFormMapper.insert(basicdataCheckForm);
				}else{
					basicdataCheckForm.preUpdate();
					basicdataCheckFormMapper.update(basicdataCheckForm);
				}
			}else{
				basicdataCheckFormMapper.delete(basicdataCheckForm);
			}
		}
	}

	@Transactional(readOnly = false)
	public void delete(BasicdataCheckStandardMain basicdataCheckStandardMain) {
		super.delete(basicdataCheckStandardMain);
		basicdataCheckFormMapper.delete(new BasicdataCheckForm(basicdataCheckStandardMain));
	}

}